﻿/*
 * Copyright © 2011 Tristan Reeves, treeves@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;

namespace Sprinkles.Attributes
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter, AllowMultiple = false)]
    public class ImportAttribute : Attribute
    {
        private readonly ImportResolutionType m_ResolutionType;
        private readonly string m_TargetReference;
        private readonly object m_LiteralValue;
        private readonly string m_SpEL;

        public ImportAttribute(ImportResolutionType resolutionType, object value)
            : this(resolutionType)
        {
            switch (resolutionType)
            {
                case ImportResolutionType.Auto:
                    break;
                case ImportResolutionType.Literal:
                    m_LiteralValue = value;
                    break;
                case ImportResolutionType.Ref:
                    m_TargetReference = (value ?? "").ToString();
                    break;
                case ImportResolutionType.SpEL:
                    m_SpEL = (value ?? "").ToString();
                    break;
                default:
                    throw new ArgumentOutOfRangeException("resolutionType");
            }
        }
        public ImportAttribute(string targetReference)
            : this(ImportResolutionType.Ref)
        {
            m_TargetReference = targetReference;
        }
        public ImportAttribute() : this(ImportResolutionType.Auto) { }
        public ImportAttribute(ImportResolutionType resolutionType)
        {
            m_TargetReference = null;
            m_SpEL = null;
            m_LiteralValue = null;
            m_ResolutionType = resolutionType;
        }
        public ImportResolutionType ResolutionType
        {
            get { return m_ResolutionType; }
        }
        public string TargetReference
        {
            get { return m_TargetReference; }
        }
        public object LiteralValue
        {
            get { return m_LiteralValue; }
        }
        public string SpEL
        {
            get { return m_SpEL; }
        }
        public object GetReferenceValue()
        {
            switch (ResolutionType)
            {
                case ImportResolutionType.Auto:
                    return null;
                case ImportResolutionType.Literal:
                    return LiteralValue;
                case ImportResolutionType.Ref:
                    return TargetReference;
                case ImportResolutionType.SpEL:
                    return SpEL;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }
}
